package JUC;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class CompletableFutureAPIDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException, TimeoutException {
        CompletableFuture<String> stringCompletableFuture = CompletableFuture.supplyAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return "abc";
        });
//        System.out.println(stringCompletableFuture.get());
//        System.out.println(stringCompletableFuture.get(2L,TimeUnit.SECONDS));
//        System.out.println(stringCompletableFuture.join());
//        System.out.println(stringCompletableFuture.getNow("xxx"));
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println(stringCompletableFuture.complete("completeValue")+"\t"+stringCompletableFuture.join());
    }
}
